package com.flowerplus.store.app.photo.loader.storage;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.flowerplus.store.app.photo.model.ImageBucket;
import com.flowerplus.store.app.photo.model.ImageItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * Created by z on.
 * Des:相册数据提供器
 * Date: 2017/3/31
 */

public class PhotoStorage {
    @Inject
    public PhotoStorage() {
    }

    public List<ImageBucket> get(Context context) {
        return get(context, true);
    }

    /**
     * 获取数据
     *
     * @param context
     * @param isDes   是否需要降序
     * @return
     */
    public List<ImageBucket> get(Context context, boolean isDes) {
        //HashMap作为临时容器，以相册名为键来对图片分类。最后转换会为List
        HashMap<String, ImageBucket> tBucketMap = new HashMap<>();
        //新建查询列
        String columns[] = new String[]{
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.BUCKET_ID,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Images.Media.DATE_ADDED};
        //新建查询
        String modified = MediaStore.Images.Media.DATE_ADDED;
        StringBuilder sb = new StringBuilder(modified);
        if (isDes) {
            sb.append(" DESC");
        }
        Cursor cur = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                columns, null, null, sb.toString());
        if (cur.moveToFirst()) {
            int indexPhotoID = cur.getColumnIndexOrThrow(columns[0]);
            int indexPhotoPath = cur.getColumnIndexOrThrow(columns[1]);
            int indexBucketID = cur.getColumnIndexOrThrow(columns[2]);
            int indexBucketDisplay = cur.getColumnIndexOrThrow(columns[3]);
            int indexPhotoDate = cur.getColumnIndexOrThrow(columns[4]);
            do {
                String id = cur.getString(indexPhotoID);
                String path = cur.getString(indexPhotoPath);
                String bucketID = cur.getString(indexBucketID);
                String bucketDisplay = cur.getString(indexBucketDisplay);
                String date = cur.getString(indexPhotoDate);
                //得到相册
                ImageBucket bucket = tBucketMap.get(bucketID);
                //如果没有该相册，则新建一个
                if (bucket == null) {
                    bucket = new ImageBucket();
                    bucket.setImageList(new ArrayList<ImageItem>());
                    bucket.setBucketName(bucketDisplay);
                    tBucketMap.put(bucketID, bucket);
                }
                //更新相册
                ImageItem image = new ImageItem();
                image.setImageId(id);
                image.setImagePath(path);
                image.setBucket(bucket);
                image.setImageDate(date);
                bucket.getImageList().add(image);
            } while (cur.moveToNext());


        }

        if (cur != null) cur.close();
        //HashMap转List
        List<ImageBucket> tmpList = new ArrayList<>();
        Iterator<Map.Entry<String, ImageBucket>> itr = tBucketMap.entrySet().iterator();
        while (itr.hasNext()) {
            Map.Entry<String, ImageBucket> entry = itr.next();
            ImageBucket value = entry.getValue();
            tmpList.add(value);
            //将相册内的第一个照片进行排序
            Collections.sort(tmpList, new Comparator<ImageBucket>() {
                @Override
                public int compare(ImageBucket o1, ImageBucket o2) {
                    if (o1.getImageList().isEmpty() || o2.getImageList().isEmpty()) {
                        return -1;
                    }
                    return o2.getImageList().get(0).getImageDate().compareTo(o1.getImageList()
                            .get(0).getImageDate());
                }
            });
        }
        return tmpList;
    }


}
